package com.bfxy.rabbitmq.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * Created by deng_wb on 2020/3/10
 */
public class RabbitUtil {

    private static final String IP = "192.168.58.58";
    private static final int PORT = 5672;
    private static final String VIRTUAL_HOST = "/";

    public static Channel consumerChannel() {
        Channel channel = null;
        try {
            // 1. 创建一个ConnectionFactory,并进行配置
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost(IP);
            connectionFactory.setPort(PORT);
            connectionFactory.setVirtualHost(VIRTUAL_HOST);
            connectionFactory.setUsername("guest");
            connectionFactory.setPassword("guest");

            connectionFactory.setAutomaticRecoveryEnabled(true);
            connectionFactory.setNetworkRecoveryInterval(3000);
            // 2. 通过连接工厂创建连接
            Connection connection = connectionFactory.newConnection();
            // 3. 通过connection创建一个Channel
            channel = connection.createChannel();
        } catch (Exception e) {
            e.printStackTrace();
        }


        return channel;
    }



    public static Channel producerChannel() {
        Channel channel = null;
        try {
            // 1. 创建一个ConnectionFactory,并进行配置
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost(IP);
            connectionFactory.setPort(PORT);
            connectionFactory.setVirtualHost(VIRTUAL_HOST);
            connectionFactory.setUsername("guest");
            connectionFactory.setPassword("guest");

            // 2. 通过连接工厂创建连接
            Connection connection = connectionFactory.newConnection();
            // 3. 通过connection创建一个Channel
            channel = connection.createChannel();
        } catch (Exception e) {
            e.printStackTrace();
        }


        return channel;
    }


}
